package se.webbzon.boltzmann.effect;

import java.awt.Color;

import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldobject.WorldStaticObject;

public class StarEffect extends TextureEffect {
	
	// The available properties for this effect
	public static final int FADE	 			= (1<<0);
	public static final int LIFTOFF				= (1<<1);
	
	// The properties for this star effect
	private final int properties;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public StarEffect(WorldStaticObject parent, int duration) {
		this(parent,duration,Color.white,0);
	}

	public StarEffect(	WorldStaticObject parent, int duration,
						Color color, float transparency) {
		this(0,parent,duration,color,transparency);
	}
	
	public StarEffect(	int starEffectProperties, WorldStaticObject parent, 
						int duration, Color color, float transparency) {
		super(parent, Resources.getTextureSet("effect_star"), duration);
		this.properties = starEffectProperties;
		if (!color.equals(Color.white))
			setColor(color);
		if (transparency != 0 && !isSet(starEffectProperties,FADE))
			setTransparency(transparency);
		if (isSet(properties,LIFTOFF))
			setVelocity(0,0.1+Math.random(),0);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void onEffectBegin(WorldArea world) {
		final WorldStaticObject parent = getParent();
		final double x = parent.getX() - parent.getWidth()/2 + parent.getWidth()*Math.random();
		final double y = parent.getY() - parent.getHeight()/2 + parent.getHeight()*Math.random();
		setPosition(x,y,parent.getZ());
	}

	@Override public void onEffectStep(WorldArea world) {
		if (isSet(properties,FADE)) {
			final float t = getDuration() / (float) getStartupDuration();
			setTransparency(t);
		}
	}

	@Override public void onEffectEnd(WorldArea world) {};
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Returns true if the given property is set in the given properties. **/
	private static final boolean isSet(int properties, int property) {
		return ((properties & property) == property);
	}

}
